package cn.yong.dao;

import cn.yong.entity.Dept;
import org.hibernate.Query;

import java.util.List;

public class DeptDao extends BaseDao {
	
	public void save(Dept dept){
		super.currentSession().save(dept);
	}

	public Dept get(int id){
		return (Dept) super.currentSession().get(Dept.class, id);
	}

	public Dept load(int id){
		return (Dept) super.currentSession().load(Dept.class, id);
	}

	public void delete(Dept dept){
		super.currentSession().delete(dept);
	}

	public void update(Dept dept){
		super.currentSession().update(dept);
	}

	public void saveOrUpdate(Dept dept){
		super.currentSession().saveOrUpdate(dept);
	}

	public Dept merge(Dept dept){
		return (Dept) super.currentSession().merge(dept);
	}


	public List<Dept> findByDeptName(String deptName){

		List<Dept> list=null;
		String hql = "from Dept where deptName=?";

		Query query = super.currentSession().createQuery(hql);

		query.setString(0,deptName);

		list = query.list();

		return list;
	}

	public List<Dept> findByDeptName2(String deptName){

		List<Dept> list=null;
		String hql = "from Dept where deptName=:name";

		Query query = super.currentSession().createQuery(hql);

		query.setString("name",deptName);


		list = query.list();

		return list;
	}

	public List<String> findAllNames() {
		String hql = "select deptName from Dept";
		return currentSession().createQuery(hql).list();
	}

	public List<Object[]> findAllDeptList() {
		String hql = "select deptNo, deptName from Dept";
		return currentSession().createQuery(hql).list();
	}

	public List<Dept> findAllDeptList_2() {
		String hql = "select new Dept(deptNo, deptName) from Dept";
		return currentSession().createQuery(hql).list();
	}

	public List<Object[]> findByInnerJoin(){
		String hql = "from Dept d inner join d.emps";
		return currentSession().createQuery(hql).list();
	}

	public List<Dept> findByInnerJoinFetch(){
		String hql = "from Dept d inner join fetch d.emps";
		return currentSession().createQuery(hql).list();
	}

	public List<Object[]> findByLeftJoin(){
		String hql = "from Dept d left join d.emps";
		return currentSession().createQuery(hql).list();
	}

	public List<Dept> findByleftJoinFetch(){
		String hql = "from Dept d left join fetch d.emps";
		return currentSession().createQuery(hql).list();
	}

	public List<Object[]> findByEqualJoin(){
		String hql = "from Dept d, Emp e where d = e.dept";
		return currentSession().createQuery(hql).list();
	}

	public Long findCount(){
		String hql = "select count(deptNo) from Dept d";
		return (Long) currentSession().createQuery(hql).uniqueResult();
	}

	public List<Dept> findBySal(){
		String hql = "from Dept d where 5000>  all(select e.sal from d.emps e)";
		return currentSession().createQuery(hql).list();
	}
}
